/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package generators;
import geography.Geography;
import data.LocalData;
import java.util.*;
import java.io.*;
import simframe.util.WeightedBucket;

/**
 *
 * @author Isaac Noble
 */
public class BankGenerator
{
    WeightedBucket<BankInfo> all_info;
    Iterator<BankInfo> iter;
    int cur_index;

    public BankGenerator() throws IOException
    {
        Geography.Build();
        all_info = new WeightedBucket<BankInfo>();
        
        InputStream in_stream = LocalData.GetInputStream(LocalData.Input.BANK_FILE);
        InputStreamReader is_reader = new InputStreamReader(in_stream);
        BufferedReader reader = new BufferedReader(is_reader);

        String line;
        line = reader.readLine();
        while ((line = reader.readLine()) != null) {

            BankInfo info = new BankInfo(line);
            all_info.add(info, info.total_assets);
        }
        reader.close();
        is_reader.close();
        in_stream.close();

        iter = all_info.iterator();
    }

    public BankInfo GetNext()
    {
        if(!iter.hasNext())
        {
            iter = all_info.iterator();
        }
        BankInfo info = iter.next();

        return info;
    }

    public static class BankInfo
    {
        public String name;
        public TreeMap<String, Geography.State> states;
        public Geography.City headquarters;
        public int certificate;
        public int fed_num;
        public int total_assets;
        public int deposits;
        public int equity;
        public boolean state_charter;

        public BankInfo()
        {
            states = new TreeMap<String, Geography.State>();
        }

        public BankInfo(String line)
        {
            String[] values = line.split(",");
            name = values[0];
            headquarters = Geography.GetCity(values[1]);
            fed_num = Integer.parseInt(values[2]);
            certificate = Integer.parseInt(values[3]);
            state_charter = Boolean.parseBoolean(values[4]);
            total_assets = Integer.parseInt(values[5]);
            deposits = Integer.parseInt(values[6]);
            equity = Integer.parseInt(values[7]);
            states = new TreeMap<String, Geography.State>();
            String[] state_codes = values[8].split(";");
            for(int i = 0; i < state_codes.length; i++)
            {
                Geography.State state = Geography.GetState(state_codes[i]);
                states.put(state.getStateCode(), state);
            }
        }

        public void AddCity(String geo_id)
        {
            Geography.State state = Geography.GetCity(geo_id).GetState();
            if(!states.containsKey(state.getStateCode()))
            {
                states.put(state.getStateCode(), state);
            }
        }

        public void AddCity(Geography.City city)
        {
            Geography.State state = city.GetState();
            if(!states.containsKey(state.getStateCode()))
            {
                states.put(state.getStateCode(), state);
            }
        }

        public String ToCSV()
        {
            String rval = name + "," + headquarters.GetGeoID() + "," + fed_num + "," + certificate + "," + state_charter + "," + total_assets + "," + deposits + "," + equity + ",";
            Iterator<String> iter = states.keySet().iterator();

            String state_string = iter.next();
            while(iter.hasNext())
            {
                String code = iter.next();
                state_string += ";" + code;
            }
            return rval + state_string;
        }

    }
}
